package com.share.common.enums;

import lombok.Getter;

import java.util.Optional;

@Getter
public enum KafkaTopicEnum {
    LOAN("loan", "用信主题", 3, 1),
    CREDIT("credit", "授信主题", 3, 1),
    PRE_LOAN("pre_loan", "用信主题", 3, 1),
    PRE_CREDIT("pre_credit", "授信主题", 3, 1),
    LOGS("logs", "日志", 3, 1),
    ORDER("order", "订单主题", 3, 1),
    TRACE("trace", "轨迹主题", 3, 1);

    //主题编码
    public final String code;

    //主题名字
    public final String name;

    //分区数
    public final int numPartitions;

    //副本数
    public short replicationFactor;

    private KafkaTopicEnum(final String code, final String name, int numPartitions, int replicationFactor) {
        this.code = code;
        this.name = name;
        this.numPartitions = numPartitions;

        //这里必须强转否则 无法使用,kafka中使用的是short定义
        this.replicationFactor = (short) replicationFactor;
    }

    public static KafkaTopicEnum toEnum(final String code) {
        for (final KafkaTopicEnum tag : KafkaTopicEnum.values()) {
            if (tag.code.equals(code)) {
                return tag;
            }
        }
        return null;
    }

    public static String toName(final String code) {
        return Optional.ofNullable(toEnum(code)).map(item -> item.name).orElse(null);
    }

}
